$(window).load(function(){
	/* 获取localstorage，填入上次信息 */
	var carOwnerInfo = JSON.parse(localStorage.getItem("eBizCarOwner"));
	if(carOwnerInfo){
		var provinceName = carOwnerInfo.truckNo.substr(0, 1);
		var provinceNum = carOwnerInfo.truckNo.substr(1, carOwnerInfo.truckNo.length-1);
		$(".home-province").text(provinceName);
		$(".home-licenseNo").val(provinceNum);
		$(".home-name").val(carOwnerInfo.ownerName);
	}



	/* 拍照识别行驶证 */
	/* 获取微信签名认证，调用接口权限 */
	var getSignatureUrl = window.location.href.split('#')[0];
	var getSignature = wxAddress + "/weixin/sign/getSignature?url="+getSignatureUrl;
	ajaxEncapsulation(getSignature, getSignatureSuc, "", "GET", "true");

	var localIds;
	var upLoadVehicleLicense = wxAddress + "/weixin/upLoadVehicleLicense";
	function getSignatureSuc(data){
		if(data.code == "0000"){
			var config = data.data;
			if(config != undefined && config != null){
				wx.config({
					debug : true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId : config.appID, // 必填，appID公众号的唯一标识
					timestamp : config.timestamp, // 必填，生成签名的时间戳
					nonceStr : config.nonceStr, // 必填，生成签名的随机串
					signature : config.signature,// 必填，签名，见附录1
					jsApiList : [ 'chooseImage','uploadImage','downloadImage' ]
				});

				$(".home-right").click(function(){
					wx.ready(function() {
						wx.chooseImage({
							count: 1, // 默认9
							sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有, 'original'
							sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
							success: function (res) {
								localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
								wx.uploadImage({
									localId: localIds, // 需要上传的图片的本地ID，由chooseImage接口获得
									isShowProgressTips: 1, // 默认为1，显示进度提示
									success: function (res) {
										wx.downloadImage({
											serverId: res.serverId, // 需要下载的图片的服务器端ID，由uploadImage接口获得
											isShowProgressTips: 1, // 默认为1，显示进度提示
											success: function (res) {
												var localId = res.localId; // 返回图片下载后的本地ID
												var upLoadVLData = {
													img: localId// 返回图片的服务器端ID
												}
												ajaxEncapsulation(upLoadVehicleLicense, upLoadVehicleLicenseSuc, upLoadVehicleLicenseErr, "POST", "true", upLoadVLData);
											}
										});
									}
								});
							}
						});
					})
				})
			}
		}else{
			warnText($(".red-bg"),"获取微信签名失败！",1000);
		}
	}
	
	function upLoadVehicleLicenseSuc(data){
		console.log(data);
		if(data.code == "0000"){
			var vlMessage = data.data.words_result;
			for(var key in vlMessage){
				if(vlMessage[key] != "" || vlMessage[key] != undefined || vlMessage[key] != null){
					switch(key){
						case "号牌号码":
							if(vlMessage[key].words.length == 7){
								$(".home-province").text(vlMessage[key].words.substring(0, 1));
								$(".home-licenseNo").val(vlMessage[key].words.substring(1, vlMessage[key].words.length));
							}
							break;
						case "所有人":
							if(vlMessage[key].words != "" || vlMessage[key].words != undefined || vlMessage[key].words != null){
								$(".home-name").val(vlMessage[key].words);
							}
							break;
						default:
							break;
					}
				}
			}
		}else{
			warnText($(".red-bg"),"获取行驶证信息失败！",1000);
		}
	}

	function upLoadVehicleLicenseErr(data){
		warnText($(".red-bg"),"获取行驶证信息失败！",1000);
	}
	
	
	
	/* 底部滚动条 */
	var mySwiper = new Swiper (".swiper-container", {
		direction: "horizontal",
		grabCursor: true,
		slidesPerView: 8,
		spaceBetween: 30,
		freeMode: true
	})



	/* 顶部渐变轮播图 */
	var slideNum = 0;
	setInterval(function(){
		slideNum++;
		if (slideNum == 3) {slideNum=0};
		$(".home-slide span").eq(slideNum).css({width: "0.37333333rem", backgroundColor: "#FFF"});
		$(".home-slide img").eq(slideNum).fadeIn();
		$(".home-slide span").eq(slideNum).siblings("span").css({width: "0.16rem", backgroundColor: "#5D4F4F"});
		$(".home-slide img").eq(slideNum).siblings("img").fadeOut();
	}, 3000)



	/* 中间关闭提示按钮 */
	$(".home-close").on("click", function(){
		$(".home-warn").hide();
	})



	/* 报价按钮规范 */
	// 车牌号只允许输入数字加英文
	$(".home-licenseNo").keyup(function() {
		$(this).val($(this).val().replace(/[\W]/g, ''));
	});

	// 限制车牌号长度及转换成大写
	$(".home-licenseNo").on("input", function() {
		if($(this).val().length > 6) {
			$(this).val($(this).val().substring(0, 6));
			return;
		}
		$(this).val($(this).val().toLocaleUpperCase());
	})

	// 回车键跳到下一个input输入框，到最后一个则直接进行报价按钮
	$("input:text").bind("keydown", function(event) {
		if(event.which == 13) {
			event.preventDefault();
			var nextinput = $("input:text")[$("input:text").index(this) + 1];
			if(nextinput != undefined) {
				nextinput.focus();
			}else {
				buttonNext();
			}
		}
	})

	// 立即报价按钮
	$(".home-next p").click(function(){
		buttonNext();
	})

	// 报价判断
	var getsaveTruckCardNew = wxAddress + "/weixin/saveTruckCardNew";
	function buttonNext(){
		$(".masklayer").show();
		$(".maskImg").show();
		var licenseNo = $(".home-province").text() + $(".home-licenseNo").val();
		var userName = $(".home-name").val().trim();
		if($(".home-licenseNo").val() == ""){
			warnText($(".red-bg"),"车牌号不能为空！",1000);
			return false;
		}else if(regs.licenseNoReg.test(licenseNo) == false){
			warnText($(".red-bg"),"车牌号格式错误！",1000);
			return false;
		}
		if(userName == ""){
			warnText($(".red-bg"),"车主姓名不能为空！",1000);
			return false;
		}else{
			// 发送ajax进行下一步
			var postData = {
				truckNo: licenseNo,
				ownerName: userName
			};
			ajaxEncapsulation(getsaveTruckCardNew, getSaveTruckCardNewSuc, getSaveTruckCardNewErr, "POST", "true", postData);
		}
	}

	// 成功获取车辆信息
	function getSaveTruckCardNewSuc(data, postValue){
		console.log(data);
		$(".masklayer").hide();
		$(".maskImg").hide();
		if(data.code == "0000"){
			var carInfo = JSON.stringify(data.data);
			window.localStorage.setItem("eBizCarInfo", carInfo);
			window.localStorage.setItem("eBizCarOwner", JSON.stringify(postValue));
			window.location.href = "bizuser.html";
		}else {
			warnText($(".red-bg"),"网络异常，错误码："+data.code,1000);
		}
	}

	// 失败获取车辆信息
	function getSaveTruckCardNewErr(data){
		$(".masklayer").hide();
		$(".maskImg").hide();
		warnText($(".red-bg"),"网络异常!",1000);
	}



	/* 选择省份功能 */
	// ajax获取省份简称，渲染DOM结构
	var getProvince = wxAddress + "/province";
	ajaxEncapsulation(getProvince, getProvinceSuc, "", "GET", "false");

	function getProvinceSuc(data){
		if(data.code == "0000"){
			var provinceInfo = data.data;
			var provinceLen = provinceInfo.length;
			var provinceList = "";
			var provinceLi = "";
			var provinceIndex = -1;
			if ((provinceLen%8) != 0) {
				provinceIndex = Math.ceil(provinceLen/8);
			}
			for (var i=0;i<provinceLen;i++) {
				if (i == 0) { // li里面第一个省份
					provinceLi = '<li class="isflexbox"><p class="isflex have-province">'+provinceInfo[i].provinceBriefName+'</p>';
				} else if (i%8 == 0) { // 下一个li里面第一个省份
					provinceLi = '</li><li class="isflexbox"><p class="isflex have-province">'+provinceInfo[i].provinceBriefName+'</p>';
				} else { // li里面中间的省份
					provinceLi = '<p class="isflex have-province">'+provinceInfo[i].provinceBriefName+'</p>';
				}
				provinceList = provinceList + provinceLi;
			}
			if (provinceIndex != -1) { // 剩余空白省份-没有省份的情况
				var noProvince = provinceIndex*8 - provinceLen;
				for (var i=0;i<noProvince;i++) {
					provinceLi = '<p class="isflex no-province"></p>';
					provinceList = provinceList + provinceLi;
				}
			}
			provinceList = provinceList + "</li>";
			$(".province-list ul").append(provinceList);
		}else{
			warnText($(".red-bg"),"获取省份信息错误！",1000);
		}
	}

	// 选择车牌号省份按钮
	$(".home-province").click(function(){
		$(".masklayer").show();
		$(".province-list").show();
	})

	// 遮罩层点击消失事件
	$(".masklayer").bind("click",function(){
		$(this).hide();
		$(".province-list").hide();
	});

	// 选择车牌号省份取消按钮
	$(".province-close").click(function(){
		$(".masklayer").hide();
		$(".province-list").hide();
	})

	// 选择车牌号省份列表里选择
	$(".province-list").delegate(".have-province", "click", function(){
		$(".home-province").text($(this).text());
		$(".masklayer").hide();
		$(".province-list").hide();
	})
})